package com.example.gmr.Activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.blankj.utilcode.util.UriUtils;
import com.bumptech.glide.Glide;
import com.example.gmr.Helper.DatabaseHelper;
import com.example.gmr.R;

public class Updata_User extends AppCompatActivity {
    private EditText  name_before,name_after,school_before,school_after,grade_before,grade_after;
    private Button commit,updata_touxiang;
    private String disp_path;
    private ImageView imageView;
    private SQLiteDatabase db;
    private DatabaseHelper databaseHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.updata_user);

        name_before=(EditText)findViewById(R.id.name_before);
        name_after=(EditText)findViewById(R.id.name_after);
        school_before=(EditText)findViewById(R.id.school_before);
        school_after=(EditText)findViewById(R.id.school_after);
        grade_before=(EditText)findViewById(R.id.grade_before);
        grade_after=(EditText)findViewById(R.id.grade_after);
        updata_touxiang=(Button)findViewById(R.id.updata_touxiang);
        commit=(Button)findViewById(R.id.commit);
        imageView=(ImageView)findViewById(R.id.imageView);
        updata_touxiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent("android.intent.action.GET_CONTENT");
                intent.setType("image/*");
                startActivityForResult(intent,22);
            }
        });
        saveUser();

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch (requestCode){
            case 22:
                Uri imageUri=data.getData();
                if (imageUri==null){
                    return;
                }
                disp_path= UriUtils.uri2File(imageUri).getPath();
                Glide.with(Updata_User.this).load(disp_path).into(imageView);
                break;
            default :
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void saveUser(){
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentValues values=new ContentValues();
                values.put("name",name_after.getText().toString());
                values.put("school",school_after.getText().toString());
                values.put("grade",grade_after.getText().toString());
                values.put("touxiang",disp_path);
                databaseHelper=new DatabaseHelper(Updata_User.this,"user",null,1);
                db=databaseHelper.getWritableDatabase();
                db.update("user",values,"name=?",new String[]{name_before.getText().toString()});

                Toast.makeText(Updata_User.this,"修改成功",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(Updata_User.this,Login.class);
                startActivity(intent);
                finish();
            }
        });
    }
}
